package com.zl.petstore.dao.mysql;

import com.zl.petstore.dao.AccountDao;
import com.zl.petstore.domain.Account;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AccountDaoImp implements AccountDao {
    @Override
    public List<Account> findAll() {
        return null;
    }

    @Override
    public Account findById(String userid) {

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Account account = null;
        //加载驱动程序
        try{
            //2.创建数据库连接
            conn = DBHelper.getConnection(); //1.加载驱动程序
            String sql = "select userid,password,email,username,addr,city,country,phone from account where userid = ?";
            //3.创建语句对象
            pstmt = conn.prepareStatement(sql);
            //4.绑定参数
            pstmt.setString(1,userid);
            //5.执行查询
            rs = pstmt.executeQuery();
            //6.遍历结果集
            if (rs.next()){
                account = new Account();

                account.setUserid(rs.getString("userid"));
                account.setPassword(rs.getString("password"));
                account.setUsername(rs.getString("username"));
                account.setEmail(rs.getString("email"));
                account.setAddr(rs.getNString("addr"));
                account.setCity(rs.getNString("city"));
                account.setCountry(rs.getNString("country"));
                account.setPhone(rs.getNString("phone"));

            }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return account;
    }

    @Override
    public int create(Account account) {
        return 0;
    }

    @Override
    public int modify(Account account) {
        return 0;
    }

    @Override
    public int remove(Account account) {
        return 0;
    }
}
